package io.renren.modules.api.controller;


import com.google.code.kaptcha.Producer;
import io.renren.common.utils.ConstantUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.ShiroUtils;
import io.renren.common.validator.Assert;
import io.renren.modules.api.annotation.AuthIgnore;
import io.renren.modules.api.entity.ApiUserEntity;
import io.renren.modules.user.entity.UserinfosEntity;
import io.renren.modules.user.service.UserinfosService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * 注册
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-26 17:27
 */
@RestController
@RequestMapping("/api")
public class ApiRegisterController {
    @Autowired
    private UserinfosService userService;
    @Autowired
    private Producer producer;


    /**
     * 注册
     */
    @AuthIgnore
    @PostMapping("/register")
    public R register(ApiUserEntity userinfos){
        Assert.isBlank(userinfos.getUseraccount(),"您的用户账户不能为空");
        Assert.isBlank(userinfos.getUsername(),"您的用户名不能为空");
        Assert.isBlank(userinfos.getPhonenum(),"您的电话号码不能为空");
        Assert.isNull(userinfos.getUsergender(),"您的性别不能为空");
        Assert.isBlank(userinfos.getUserpwd(),"您的密码不能为空");
        Assert.isBlank(userinfos.getAddressinfo(),"您的地址不能为空");
        Assert.isBlank(userinfos.getCaptcha(),"您的验证码不能为空");
        //验证电话格式
        Assert.phoneVerify(userinfos.getPhonenum());
        //验证密码是否符合规则
        Assert.passwordVerify(userinfos.getUserpwd());

        //判断输入值是否符合规则
        //性别
        R usergenderError = userService.judgeIsUsergenderError(userinfos.getUsergender());
        if (usergenderError != null){
            return usergenderError;
        }
        if (userinfos.getUseraccount().length() < 4){
            return R.error("用户账户少于4位");
        }
        int count = userService.isUserAccountRepetition(userinfos.getUseraccount());
        if (count > 0){
            return R.error("该用户账户已存在");
        }
        String kaptcha = ShiroUtils.getKaptcha(ConstantUtils.LOGIN_KAPTCHA_SESSION_KEY);
        if(!userinfos.getCaptcha().equalsIgnoreCase(kaptcha)){
            return R.error("验证码不正确");
        }
        //软删除和锁定默认值
        userinfos.setIsdel(ConstantUtils.DEL);
        userinfos.setIslockedout(ConstantUtils.ISLOCKEDOUT);
        userService.save(userinfos);
        return R.ok("注册成功");
    }

    @AuthIgnore
    @RequestMapping("register/captcha.jpg")
    public void captcha(HttpServletResponse response)throws IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        //生成文字验证码
        String text = producer.createText();
        //生成图片验证码
        BufferedImage image = producer.createImage(text);

        //保存到shiro session
        ShiroUtils.setSessionAttribute(ConstantUtils.REGISTER_KAPTCHA_SESSION_KEY, text);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        IOUtils.closeQuietly(out);
    }
}
